﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
namespace SGUI
{
    public class SGUIFont
    {
        private Material Mat;
        public Font font;
        public FontStyle fontStyle = FontStyle.Normal;
        public int pixelSize { get { return font.material.mainTexture.width; } }
        CharacterInfo mChar;
        CharacterInfo mTemp;
        float FontOffset;
        public void RequestText(string text, int fontSize)
        {
            if (font != null)
            {
                font.RequestCharactersInTexture("j", fontSize, fontStyle);
                font.GetCharacterInfo('j', out mTemp, fontSize, fontStyle);
                FontOffset = (fontSize + mTemp.vert.yMax);
                font.RequestCharactersInTexture(text, fontSize, fontStyle);
            }
        }

        public Vector2 CalculatePrintedSize(string text, int fontSize)
        {
            int x = 0;
            int y = 0;
            for (int i = 0; i < text.Length; ++i)
            {
                char c = text[i];
                if (font.GetCharacterInfo(c, out mChar, fontSize, fontStyle))
                {
                    x += (int)(mChar.width);
                }
            }
            return new Vector2(x, y + fontSize);
        }

        public void Print(string text, int fontSize, Color32 color, List<Vector3> verts, List<Vector2> uvs, List<Color> cols, float lineWidth,Vector2 pv)
        {
            Vector3 v0 = Vector3.zero, v1 = Vector3.zero;
            Vector2 u0 = Vector2.zero, u1 = Vector2.zero;
            int x = 0;
            int y = 0;
            for (int i = 0; i < text.Length; i++)
            {
                char chr = text[i];
                font.GetCharacterInfo(chr, out mChar, fontSize);
                v0.x = (x + mChar.vert.xMin) - (pv.x * lineWidth);
                v0.y = -(y - mChar.vert.yMax + FontOffset) + (fontSize) - (fontSize * pv.y);

                v1.x = v0.x + mChar.vert.width;
                v1.y = v0.y - mChar.vert.height;

                u0.x = mChar.uv.xMin;
                u0.y = mChar.uv.yMin;
                u1.x = mChar.uv.xMax;
                u1.y = mChar.uv.yMax;

                x += (int)mChar.width;
                for (int b = 0; b < 4; ++b) cols.Add(color);

                if (mChar.flipped)
                {
                    uvs.Add(new Vector2(u0.x, u1.y));
                    uvs.Add(new Vector2(u0.x, u0.y));
                    uvs.Add(new Vector2(u1.x, u0.y));
                    uvs.Add(new Vector2(u1.x, u1.y));
                }
                else
                {
                    uvs.Add(new Vector2(u1.x, u0.y));
                    uvs.Add(new Vector2(u0.x, u0.y));
                    uvs.Add(new Vector2(u0.x, u1.y));
                    uvs.Add(new Vector2(u1.x, u1.y));
                }


                verts.Add(new Vector3(v1.x, v0.y));
                verts.Add(new Vector3(v0.x, v0.y));
                verts.Add(new Vector3(v0.x, v1.y));
                verts.Add(new Vector3(v1.x, v1.y));
            }
        }
    }

}